<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Content_model extends CI_Model {

	public $content_tbl		= 'contents';
	public $contentcat_tbl	= 'content_categories';
	public $websites_tbl	= 'websites';
	public $contentweb_tbl	= 'content_websites';
	public $userweb_tbl 	= 'user_websites';
	public $cat_tbl			= 'content_categories';
	public $poll_tbl		= 'polls';
	public $pollans_tbl		= 'poll_answers';
	public $prodcat_tbl		= 'website_product_categories';
	public $prodlabel_tbl	= 'website_product_labels';
	public $prod_tbl		= 'website_products';
	public $websites		= '';
	
	// ------------------------------------------------------------------------
	
	public function __construct()
	{
		parent::__construct();
		
		$this->websites = explode(',', $this->session->userdata('websites'));
	}
	
	// ------------------------------------------------------------------------
	
	public function create_content($fields)
	{
		$fields['slug']					= $this->_check_slug($fields['title'], $fields['site_code']);
		$fields['created'] 				= date('Y-m-d H:i:s');
		$fields['created_by']			= $this->session->userdata('user_id');
		
		$this->db->insert($this->content_tbl, $fields);
		
		return $this->db->affected_rows() > 0 ? $this->get_content($this->db->insert_id()) : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function _check_slug($slug, $site_code)
	{
		$str 	= url_title($slug, 'dash', TRUE);
		$where 	= array('slug' => $str, 'site_code' => $site_code);

		do {	
			if ($content = $this->get_content($where, '', 'content_id'))
			{
				$str = increment_string($str, '-');
			}
		} while ($content !== FALSE);
		
		return $str;
	}
	
	// ------------------------------------------------------------------------
	
	public function get_content($value, $where = 'content_id', $params = '*', $user = FALSE)
	{
		if (is_array($value)) $this->db->where($value);
		else $this->db->where($where, $value);
		
		if ($user) $this->db->where_in('c.site_code', $this->websites);
		
		$query = $this->db->select($params)
						  ->join($this->contentcat_tbl . ' AS cc', 'cc.content_category_id = c.content_category_id', 'left')
						  ->where('c.deleted', 'n')
						  ->get($this->content_tbl . ' AS c', 1);
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function create_content_websites($content_id, $fields)
	{
		if (! $fields) return TRUE;
		
		foreach ($fields as $site)
		{
			$data[] = array(
				'site_code'		=> $site,
				'content_id'	=> $content_id
			);
		}
		
		return $this->db->insert_batch($this->contentweb_tbl, $data);
	}
	
	// ------------------------------------------------------------------------
	
	public function update_content($fields, $content_id)
	{
		$fields['updated'] = date('Y-m-d H:i:s');
		
		$query = $this->db->where('content_id', $content_id)
						  ->where('deleted', 'n')
						  ->update($this->content_tbl, $fields);
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function delete_content($content_id)
	{
		$fields['deleted'] = 'y';
		
		$this->update_content($fields, $content_id);
		
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function list_child_categories($category_id)
	{
		$query = $this->db->where('ancestor', $category_id)
						  ->order_by('parent_category, name')
						  ->get($this->cat_tbl);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function list_contents($limit = '', $offset = '', $order = 'c.created', $where = '', $params = '*', $user = FALSE)
	{
		$offset = $offset > 0 ? ($offset - 1) * $limit : 0;
		
		if ($params == '*') $this->db->select('c.*, cat.name AS category_name, cat.ancestor AS ancestor_id, cat2.name AS parent_cat_name, cat3.name AS ancestor_name');
		
		if (is_array($where)) $this->db->where($where);
		
		if ($user)
		{
			$this->db->select('w.site_name')
					 ->join($this->websites_tbl . ' AS w', 'w.site_code = c.site_code', 'left')
					 ->where_in('c.site_code', $this->websites);
		}
		
		$query = $this->db->join($this->cat_tbl . ' AS cat', 'cat.content_category_id = c.content_category_id', 'left')
						  ->join($this->cat_tbl . ' AS cat2', 'cat2.content_category_id = cat.parent_category', 'left')
						  ->join($this->cat_tbl . ' AS cat3', 'cat3.content_category_id = cat.ancestor', 'left')
						  ->where('c.deleted', 'n')
						  ->order_by($order)
						  ->get($this->content_tbl . ' AS c', $limit, $offset);
		
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function count_contents($where = '', $user = FALSE)
	{
		if (is_array($where)) $this->db->where($where);
		
		if ($user) $this->db->where_in('c.site_code', $this->websites);
		
		$query = $this->db->select('content_id')
						  ->join($this->cat_tbl . ' AS cat', 'cat.content_category_id = c.content_category_id', 'left')
						  ->where('c.deleted', 'n')
						  ->get($this->content_tbl . ' AS c');
						  
		return $query->num_rows();
	}
	
	// ------------------------------------------------------------------------
	
	public function list_categories($where = '', $order = '')
	{
		if ($where) $this->db->where($where);
		
		if ($order) $this->db->order_by($order);
		
		$query = $this->db->where('deleted', 'n')
						  ->get($this->cat_tbl);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function create_category($fields)
	{
		$this->db->insert($this->cat_tbl, $fields);
		
		return $this->db->affected_rows() > 0 ? $this->get_category($this->db->insert_id()) : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function get_category($category_id)
	{
		$query = $this->db->where('content_category_id', $category_id)
						  ->where('deleted', 'n')
						  ->get($this->cat_tbl, 1);
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function update_category($fields, $category_id)
	{
		$query = $this->db->where('content_category_id', $category_id)
						  ->where('deleted', 'n')
						  ->update($this->cat_tbl, $fields);
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function delete_category($category_id)
	{
		$query = $this->db->where('content_category_id', $category_id)
						  ->where('deleted', 'n')
						  ->update($this->cat_tbl, array('deleted' => 'y'));
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function create_poll($fields, $answers)
	{
		$fields['created'] 		= date('Y-m-d H:i:s');
		$fields['created_by']	= $this->session->userdata('user_id');
		
		$this->db->insert($this->poll_tbl, $fields);
		$poll_id = $this->db->insert_id();
		
		$this->create_poll_answers($answers, $poll_id);
		
		return $poll_id;
	}
	
	// ------------------------------------------------------------------------
	
	public function create_poll_answers($fields, $poll_id)
	{
		foreach ($fields as $answer)
		{
			$data[] = array(
				'answer'	=> $answer,
				'poll_id'	=> $poll_id
			);
		}
		
		if (! $data) return TRUE;
		
		return $this->db->insert_batch($this->pollans_tbl, $data);
	}
	
	// ------------------------------------------------------------------------
	
	public function get_poll($value, $where = 'poll_id', $params = '*', $user = FALSE)
	{
		if (is_array($value)) $this->db->where($value);
		else $this->db->where($where, $value);
		
		if ($user) $this->db->where_in('p.site_code', $this->websites);
		
		$query = $this->db->select($params)
						  ->where('deleted', 'n')
						  ->get($this->poll_tbl . ' AS p', 1);
						  
		if ($query->num_rows() > 0)
		{
			$data = $query->row_array();
			
			if ($params == '*') $data['answers'] = $this->get_poll_answers($value, $where);
			
			return $data;
		}
		
		return FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function get_poll_answers($value, $where = 'poll_id')
	{
		$query = $this->db->where($where, $value)
						  ->where('deleted', 'n')
						  ->order_by('position')
						  ->get($this->pollans_tbl);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function update_poll($fields, $answers, $poll_id)
	{
		$fields['updated'] = date('Y-m-d H:i:s');
		
		$query = $this->db->where('poll_id', $poll_id)
						  ->where('deleted', 'n')
						  ->update($this->poll_tbl, $fields);
						  
		$this->update_poll_answers($answers, $poll_id);
		
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function update_poll_answers($fields, $poll_id)
	{
		$ans = FALSE;
		$fields = json_decode($fields, TRUE);
		
		foreach ($fields as $key => $answer)
		{
			$data = array(
				'answer'	=> $answer['answer'],
				'position'	=> $key + 1
			);
			
			if (isset($answer['answer_id']))
			{
				if (isset($answer['deleted'])) $data['deleted'] = 'y';
				
				$this->db->where('poll_answer_id', $answer['answer_id'])
						 ->where('deleted', 'n')
						 ->update($this->pollans_tbl, $data);
			}
			else
			{
				if (! isset($answer['deleted']))
				{
					$data['poll_id'] = $poll_id;
					
					$ans[] = $data;
				}
			}
			
			$data = '';
		}
		
		return $ans ? $this->db->insert_batch($this->pollans_tbl, $ans) : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function list_polls($limit = '', $offset = '', $order = 'c.created', $where = '', $params = '*', $user = FALSE)
	{
		$offset = $offset > 0 ? ($offset - 1) * $limit : 0;
		
		if (is_array($where)) $this->db->where($where);
		
		if ($params == '*')
		{
			$sub = '(SELECT COUNT(poll_answer_id) FROM ' . $this->pollans_tbl . ' WHERE poll_id = p.poll_id AND deleted = "n") AS answers_count';
			$this->db->select('p.*, w.site_name, ' . $sub, FALSE);
		}
		
		if ($user) $this->db->where_in('p.site_code', $this->websites);
		
		$query = $this->db->join($this->websites_tbl . ' AS w', 'w.site_code = p.site_code', 'left')
						  ->where('p.deleted', 'n')
						  ->order_by($order)
						  ->get($this->poll_tbl . ' AS p', $limit, $offset);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function count_polls($user = FALSE)
	{
		if ($user) $this->db->where_in('site_code', $this->websites);
		
		$query = $this->db->select('poll_id')
						  ->where('deleted', 'n')
						  ->get($this->poll_tbl);
						  
		return $query->num_rows();
	}
	
	// ------------------------------------------------------------------------
	
	public function delete_poll($poll_id)
	{
		$query = $this->db->where('poll_id', $poll_id)
						  ->where('deleted', 'n')
						  ->update($this->poll_tbl, array('deleted' => 'y'));
						  
		$query = $this->db->where('poll_id', $poll_id)
						  ->where('deleted', 'n')
						  ->update($this->pollans_tbl, array('deleted' => 'y'));
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function create_product_category($fields)
	{
		$fields['created'] = date('Y-m-d H:i:s');
		
		$this->db->insert($this->prodcat_tbl, $fields);
		
		return $this->db->affected_rows() > 0 ? $this->get_product_category($this->db->insert_id()) : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function get_product_category($value, $where = 'site_prod_cat_id')
	{
		$query = $this->db->where($where, $value)
						  ->where('deleted', 'n')
						  ->get($this->prodcat_tbl, 1);
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function list_product_categories($limit = '', $offset = '', $order = 'c.created', $where = '', $params = '*', $user = FALSE)
	{
		$offset = $offset > 0 ? ($offset - 1) * $limit : 0;
		
		if ($where) $this->db->where($where);
		
		if ($user) $this->db->where_in('c.site_code', $this->websites);
		
		$query = $this->db->select('c.*, w.site_name')
						  ->join($this->websites_tbl . ' AS w', 'w.site_code = c.site_code', 'left')
						  ->where('c.deleted', 'n')
						  ->get($this->prodcat_tbl . ' AS c', $limit, $offset);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function count_product_categories($user = FALSE)
	{
		if ($user) $this->db->where_in('site_code', $this->websites);
		
		$query = $this->db->select('site_prod_cat_id')
						  ->where('deleted', 'n')
						  ->get($this->prodcat_tbl);
						  
		return $query->num_rows();
	}
	
	// ------------------------------------------------------------------------
	
	public function update_product_category($fields, $cat_id)
	{
		$query = $this->db->where('site_prod_cat_id', $cat_id)
						  ->where('deleted', 'n')
						  ->update($this->prodcat_tbl, $fields);
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function delete_product_category($cat_id)
	{
		$fields['deleted'] = 'y';
		
		$this->update_product_category($fields, $cat_id);
		
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function create_product_label($fields)
	{
		$fields['created'] = date('Y-m-d H:i:s');
		
		$this->db->insert($this->prodlabel_tbl, $fields);
		
		return $this->db->affected_rows() > 0 ? $this->get_product_label($this->db->insert_id()) : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function get_product_label($value, $where = 'site_prod_label_id')
	{
		$query = $this->db->select('pl.*, pc.site_code')
						  ->join($this->prodcat_tbl . ' AS pc', 'pc.site_prod_cat_id = pl.site_prod_cat_id', 'left')
						  ->where($where, $value)
						  ->where('pl.deleted', 'n')
						  ->get($this->prodlabel_tbl . ' AS pl', 1);
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function list_product_labels($limit = '', $offset = '', $order = 'pl.name', $where = '', $params = '*', $user = FALSE)
	{
		$offset = $offset > 0 ? ($offset - 1) * $limit : 0;
		
		if ($where) $this->db->where($where);
		
		if ($params == '*') $params = 'pl.*, pc.name AS category_name, pc.site_code, w.site_name';
		
		if ($user) $this->db->where_in('pc.site_code', $this->websites);
		
		$query = $this->db->select($params)
						  ->join($this->prodcat_tbl . ' AS pc', 'pc.site_prod_cat_id = pl.site_prod_cat_id', 'left')
						  ->join($this->websites_tbl . ' AS w', 'w.site_code = pc.site_code', 'left')
						  ->where('pl.deleted', 'n')
						  ->order_by($order)
						  ->get($this->prodlabel_tbl . ' AS pl', $limit, $offset);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function count_product_labels($user = FALSE)
	{
		if ($user) $this->db->where_in('pc.site_code', $this->websites);
		
		$query = $this->db->select('pl.site_prod_cat_id')
						  ->join($this->prodcat_tbl . ' AS pc', 'pc.site_prod_cat_id = pl.site_prod_cat_id', 'left')
						  ->where('pl.deleted', 'n')
						  ->get($this->prodlabel_tbl . ' AS pl');
						  
		return $query->num_rows();
	}
	
	// ------------------------------------------------------------------------
	
	public function create_product($fields)
	{
		$fields['created'] = date('Y-m-d H:i:s');
		
		$this->db->insert($this->prod_tbl, $fields);
		
		return $this->db->affected_rows() > 0 ? $this->get_product($this->db->insert_id()) : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function get_product($value, $where = 'site_product_id', $params = '*')
	{
		if ($params == '*') $params = 'p.*, pc.site_code, pl.name AS label_name, pc.name AS category_name, w.site_name';
		
		$query = $this->db->select($params)
						  ->join($this->prodlabel_tbl . ' AS pl', 'pl.site_prod_label_id = p.site_prod_label_id AND pl.deleted = "n"', 'left')
						  ->join($this->prodcat_tbl . ' AS pc', 'pc.site_prod_cat_id = pl.site_prod_cat_id AND pc.deleted = "n"', 'left')
						  ->join($this->websites_tbl . ' AS w', 'w.site_code = pc.site_code AND w.deleted = "n"', 'left')
						  ->where($where, $value)
						  ->where('p.deleted', 'n')
						  ->get($this->prod_tbl . ' AS p', 1);
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function list_products($limit = '', $offset = '', $order = 'p.name', $where = '', $params = '*', $user = FALSE)
	{
		$offset = $offset > 0 ? ($offset - 1) * $limit : 0;
		
		if ($where) $this->db->where($where);
		
		if ($params == '*') $params = 'p.*, pl.name AS label_name, pc.name AS category_name, w.site_name';
		
		if ($user) $this->db->where_in('pc.site_code', $this->websites);
		
		$query = $this->db->select($params)
						  ->join($this->prodlabel_tbl . ' AS pl', 'pl.site_prod_label_id = p.site_prod_label_id AND pl.deleted = "n"', 'left')
						  ->join($this->prodcat_tbl . ' AS pc', 'pc.site_prod_cat_id = pl.site_prod_cat_id AND pc.deleted = "n"', 'left')
						  ->join($this->websites_tbl . ' AS w', 'w.site_code = pc.site_code AND w.deleted = "n"', 'left')
						  ->where('p.deleted', 'n')
						  ->order_by($order)
						  ->get($this->prod_tbl . ' AS p', $limit, $offset);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function count_products($user = FALSE)
	{
		if ($user) $this->db->where_in('pc.site_code', $this->websites);
		
		$query = $this->db->select('p.site_product_id')
						  ->join($this->prodlabel_tbl . ' AS pl', 'pl.site_prod_label_id = p.site_prod_label_id AND pl.deleted = "n"', 'left')
						  ->join($this->prodcat_tbl . ' AS pc', 'pc.site_prod_cat_id = pl.site_prod_cat_id AND pc.deleted = "n"', 'left')
						  ->where('p.deleted', 'n')
						  ->get($this->prod_tbl . ' AS p');
						  
		return $query->num_rows();
	}
	
	// ------------------------------------------------------------------------
	
	public function update_product($fields, $prod_id)
	{
		$fields['updated'] = date('Y-m-d H:i:s');
		
		$query = $this->db->where('site_product_id', $prod_id)
						  ->where('deleted', 'n')
						  ->update($this->prod_tbl, $fields);
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function delete_product($prod_id)
	{
		$fields['deleted'] = 'y';
		
		$this->update_product($fields, $prod_id);
		
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function update_product_label($fields, $label_id)
	{
		$fields['updated'] = date('Y-m-d H:i:s');
		
		$query = $this->db->where('site_prod_label_id', $label_id)
						  ->where('deleted', 'n')
						  ->update($this->prodlabel_tbl, $fields);
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function delete_product_label($label_id)
	{
		$fields['deleted'] = 'y';
		
		$this->update_product_label($fields, $label_id);
		
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
}

/* End of file content_model.php */
/* Location: ./application/models/content_model.php */